# -*- coding: utf-8 -*-
import logging, random, re
import concurrent.futures
from urllib.parse import urlparse

import requests

from django.core.management.base import BaseCommand
from django.conf import settings

from novel.book_robot import SearchOption, \
    Net99libEbookRobot

from novel.robot_mixin import HttpMixin, ExecutorMixin, PersistenceMixin

log = logging.getLogger('book.hetu')


class Command(BaseCommand, HttpMixin, ExecutorMixin, PersistenceMixin):
    help = 'fetch the specified book from www.99lib.net'    
    images = []
    chapter_sort_no_re = re.compile(r'[A-Z]+%')
    img_re = re.compile(r'<img src="([\s\S]+?)"')
    chapter_div_re = re.compile(r'<div>([\s\S]+?)</div>')
    sorted_chapter_div_re = re.compile(
        r'<div class="chapter">([\s\S]+?)</div>')
    book_inner_link_re = re.compile(
        r'<a href="/book/\d+/index.htm">([\s\S]+?)</a>')
    remove_tags_re = [re.compile(r'<%s>[\s\S]+?</%s>' % (x, x))
                      for x in "acronym, bdo, big, cite, code, "
                      "dfn, kbd, q, s, samp, strike, tt, u, var".split(', ')]
    content_re = re.compile(r'<div id="content">([\s\S]+)</div>')    
    def add_arguments(self, parser):
        parser.add_argument('--start', type=int, default=1,
                            help='start page num')
        parser.add_argument('--end', type=int, default=3, help='end page num')
        parser.add_argument('--c', type=int, default=2,
                            help='concurrent workers count')
        parser.add_argument('--m', type=str, default='thread',
                            help='thread or process')
        parser.add_argument('--site', type=str, default='99lib',
                            help='search site(99lib[9] or hetushu[h] '
                            'or csw[w])')
        parser.add_argument('--book_id', nargs='+', type=int)

    def compose_urls(self, **options):
        urls = {
            'action':None,            
        }
        site = options['site']
        keys = options['book_id']
        if keys:
            ep_urls = {
                '99lib': 'http://www.99lib.net/book/{key}/index.htm',
                'hetushu': 'http://www.hetushu.com/book/{key}/index.html',
                'csw': 'http://book.99csw.com/book/{key}/index.htm'
            }
            urls['action'] = 'handle_book'
        else:
            keys = range(options['end'],options['start'],-1)
            ep_urls = {
                'hetushu':'http://www.hetushu.com/book/index.php?page={key}',
            }       
            urls['action'] = 'handle_page'     
                   
        if site == '99lib' or site == '9':            
            urls['urls'] = {book_id: ep_urls['99lib'].format(key=book_id)
                    for book_id in keys}
        elif site == 'hetushu' or site == 'h':            
            urls['urls'] = {book_id: ep_urls['hetushu'].format(
                key=book_id) for book_id in keys}
        elif site == 'csw' or site == 'w':            
            urls['urls'] = {book_id: ep_urls['csw'].format(
                key=book_id) for book_id in keys}
        else:
            self.stdout.write(self.style.error('invalid site %s' % (site,)))
            
        return urls
    
    def handle(self, *args, **options):                
        self.images = []
        urls = self.compose_urls(**options)

        executor_class = self.get_executor_class(options['m'])
        with executor_class(max_workers=options['c']) as executor:
            future_to_url = {executor.submit(
                urls['action'], book_id, urls['urls'][book_id]): book_id
                for book_id in urls['urls']}
            for future in concurrent.futures.as_completed(future_to_url):
                url = future_to_url[future]
                future.result()
                self.log.debug('OK   %s', url)                